package com.example.android2019.homework.homework4;

import android.content.DialogInterface;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.android2019.R;
import com.example.android2019.homework.homework6.FileSave;

import java.text.DecimalFormat;
import java.util.HashMap;

public class BMIActivity extends AppCompatActivity {
    EditText ch2_bmi_ed_height, ch2_bmi_ed_weight;
    Button ch2_bmi_btn_computer, ch2_bmi_btn_about, ch2_bmi_btn_exit, ch2_bmi_btn_SharedPreference, ch2_bmi_btn_sd;
    TextView ch2_bmi_tv_result;

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bmi);
        /*关联控件*/
        inti();
        View.OnClickListener clickListener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                /*View v这个参数表示点击的是哪个控件*/
                switch (v.getId()) {
                    case R.id.ch2_bmi_btn_computer:
                        computer();
                        break;
                    case R.id.ch2_bmi_btn_about:
                        about();
                        break;
                    case R.id.ch2_bmi_btn_exit:
                        exit();
                        break;
                    case R.id.ch2_bmi_btn_SharedPreference:
                        sp();
                        break;
                    case R.id.ch2_bmi_btn_sd:
                        sd();
                        break;
                }
            }
        };
        ch2_bmi_btn_computer.setOnClickListener(clickListener);
        ch2_bmi_btn_about.setOnClickListener(clickListener);
        ch2_bmi_btn_exit.setOnClickListener(clickListener);
        ch2_bmi_btn_SharedPreference.setOnClickListener(clickListener);
        ch2_bmi_btn_sd.setOnClickListener(clickListener);
        /*跳转*/
        skip();
    }

    private void sd() {
       /* 用文件外部存储，先动态申请权限*/
        String[] p = {"android.permission.WRITE_EXTERNAL_STORAGE", "android.permission.READ_EXTERNAL_STORAGE"};
        ActivityCompat.requestPermissions(BMIActivity.this, p, 1);
        /* 从sp/文件中获取账号密码并显示在输入框 */
        HashMap<String, String> hashMap = FileSave.get(BMIActivity.this);
        ch2_bmi_ed_height.setText(hashMap.get("height"));
        ch2_bmi_ed_weight.setText(hashMap.get("weight"));

        View.OnClickListener listener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String height = ch2_bmi_ed_height.getText().toString();
                String weight = ch2_bmi_ed_weight.getText().toString();
                if (TextUtils.isEmpty(height) || TextUtils.isEmpty(weight)) {
                    Toast.makeText(BMIActivity.this, "身高或体重不能为空", Toast.LENGTH_SHORT).show();
                } else {
                    boolean flag = FileSave.save(BMIActivity.this, height, weight);
                    if (flag) {
                        Toast.makeText(BMIActivity.this, "保存成功", Toast.LENGTH_SHORT).show();
                    } else {
                        Toast.makeText(BMIActivity.this, "保存失败", Toast.LENGTH_SHORT).show();
                    }
                }
            }
        };
        ch2_bmi_btn_sd.setOnClickListener(listener);
    }

    private void sp() {

    }

    private void inti() {
        ch2_bmi_btn_computer = findViewById(R.id.ch2_bmi_btn_computer);
        ch2_bmi_btn_about = findViewById(R.id.ch2_bmi_btn_about);
        ch2_bmi_btn_exit = findViewById(R.id.ch2_bmi_btn_exit);
        ch2_bmi_ed_height = findViewById(R.id.ch2_bmi_ed_height);
        ch2_bmi_ed_weight = findViewById(R.id.ch2_bmi_ed_weight);
        ch2_bmi_tv_result = findViewById(R.id.ch2_bmi_tv_result);
        ch2_bmi_btn_SharedPreference = findViewById(R.id.ch2_bmi_btn_SharedPreference);
        ch2_bmi_btn_sd = findViewById(R.id.ch2_bmi_btn_sd);
    }

    private void exit() {
        DialogInterface.OnClickListener listener = new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                BMIActivity.this.finish();
            }
        };
        AlertDialog.Builder dialog = new AlertDialog.Builder(BMIActivity.this);
        dialog.setTitle("退出?");
        dialog.setMessage("真的要退出BMI系统吗？");
        dialog.setPositiveButton("确定", listener);
        dialog.setNegativeButton("取消", null);
        dialog.show();
    }

    private void about() {
        /*跳转*/
        Intent intent = new Intent(BMIActivity.this, AboutActivity.class);
        /*传递数据*/
        intent.putExtra("test", "abcde");
        startActivity(intent);
    }

    private void computer() {
        String s_height = ch2_bmi_ed_height.getText().toString();
        double height = Double.parseDouble(s_height);
        height = height / 100;
        String s_weight = ch2_bmi_ed_weight.getText().toString();
        double weight = Double.parseDouble(s_weight);
        double result = weight / height / height;
        /*结果保留两位小数*/
        DecimalFormat format = new DecimalFormat("0.00");
        String s = "您的体重指数为：" + format.format(result) + "\n理想指数为：20-22\n";
        if (result < 18.5) {
            s += "过轻";
        } else if (result < 23) {
            s += "正常";
        } else if (result < 25) {
            s += "过重";
        } else if (result < 30) {
            s += "肥胖";
        } else {
            s += "痴肥";
        }
        ch2_bmi_tv_result.setText(s);
    }

    private void skip() {
        /*获取传递的数据*/
        Intent intent = this.getIntent();
        String a = intent.getStringExtra("test");
        Toast.makeText(this, a, Toast.LENGTH_SHORT).show();
    }
}
